package com.imooc.web;

import com.imooc.dto.ProductCategoryExecution;
import com.imooc.entity.ProductCategory;
import com.imooc.entity.Shop;
import com.imooc.enums.ProductCategoryStateEnum;
import com.imooc.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author
 * @create 2018-08-02 20:35
 **/
@Controller
@RequestMapping("/shop")
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;


    /**
     * 删除商品分类
     * @param productCategoryId
     * @param request
     * @return
     */
    @RequestMapping(value = "/removeproductcategory", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> removeProductCategory(int productCategoryId, HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();

        if (productCategoryId > 0) {
            try {
                //从session中获取当前的店铺id
                Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
                //将shopid和productCategoryId传给serivce实现
                ProductCategoryExecution productCategoryExecution = productCategoryService.deleteProductCategory(productCategoryId, currentShop.getShopId());
                if (productCategoryExecution.getState() == ProductCategoryStateEnum.SUCCESS.getState()) {
                    //返回给前端数据
                    modelMap.put("success", true);
                }else {
                    modelMap.put("success", false);
                    modelMap.put("errMsg", productCategoryExecution.getStateInfo());
                }
            } catch (Exception e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.toString());
            }
        }else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "删除商品失败");
        }
        return modelMap;
    }


    /**
     * 获取店铺的所有商品分类信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/listproductcategorys")
    @ResponseBody
    public Map<String, Object> listProductCategorys(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        List<ProductCategory> productCategoryList = new ArrayList<ProductCategory>();
        //应该从request获取 TODO
        int shopId = 3;
        //获取商品分类信息
        productCategoryList = productCategoryService.getProductCategoryList(shopId);
        if (productCategoryList != null && productCategoryList.size() > 0) {
            modelMap.put("success", true);
            modelMap.put("data", productCategoryList);

        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "获取店铺信息失败");
        }
        return modelMap;
    }

    /**
     * 批量添加用户类别
     *
     * @param productCategoryList
     * @param request
     * @return
     */
    @RequestMapping(value = "/addproductcategorys", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addProductCategorys(@RequestBody List<ProductCategory> productCategoryList,
                                                   HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        //从session中获取当前的shopid值
        Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
        for (ProductCategory productCategory : productCategoryList) {
            productCategory.setShopId(currentShop.getShopId());
            productCategory.setCreateTime(new Date());
        }
        if (productCategoryList != null && productCategoryList.size() > 0) {
            try {
                ProductCategoryExecution productCategoryExecution = productCategoryService.batchAddProductCategory(productCategoryList);
                if (productCategoryExecution.getState() == ProductCategoryStateEnum.SUCCESS.getState()) {
                    modelMap.put("success", true);
                } else {
                    modelMap.put("success", false);
                    modelMap.put("errMsg", productCategoryExecution.getStateInfo());
                }
            } catch (Exception e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.toString());
                return modelMap;
            }
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "请至少输入一个商品类别");
        }
        return modelMap;
    }


}
